﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMWJamecl.Repository;
using SMWJamecl.BusinessEntities;

namespace SMWJamecl.Repository
{
    public class ModeloRepository:IModeloRepository
    {
        SMWJameclContext context;

        public ModeloRepository()
        {
            if (context == null)
            {
                context = new SMWJameclContext();
            }
        }

        public List<Modelo> GetAllFromModelos()
        {
            var query = from c in context.Modeloes
                        select c;
            return query.ToList();
        }


        public Modelo GetAllFromModeloId(int id)
        {
            var query = from c in context.Modeloes
                        where c.Id_Modelo.Equals(id)
                        select c;
            return query.SingleOrDefault();
        }

        public void AddModelo(Modelo modelo)
        {
            context.Modeloes.Add(modelo);
            context.SaveChanges();
        }

        public void Edit(Modelo modelo)
        {
            context.Entry(modelo).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void Remove(int id)
        {
            var existe = context.Modeloes.Find(id);

            if (existe != null)
            {
                context.Modeloes.Remove(existe);
                context.SaveChanges();
            }
        }
    }
}
